import java.io.ByteArrayOutputStream;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;

public class MemoryTester {

	public static void main(String[] args) {

		String s1 = "test string";
		getObjectDump(s1, false);
		
	}

	public static void getObjectDump( Object obj, boolean fDeep ) { 

		if (obj == null) { 
			System.out.println( "object passed is null!" ); 
			return;
		}

		Class sessClass = obj.getClass();
		System.out.println("Class name = "+ sessClass.getName());    

		int sessObjSize = 0; 

		try { 
			ByteArrayOutputStream baSessStrm = new ByteArrayOutputStream(); 
			ObjectOutputStream objSessStrm = new ObjectOutputStream(baSessStrm ); 
			objSessStrm.writeObject( obj ); 
			objSessStrm.close(); 
			sessObjSize = baSessStrm.size(); 
			System.out.println("Serialized size = "+ sessObjSize + " bytes");
		} 
		catch (Exception e) { 
			if (e instanceof NotSerializableException) { 
				System.err.println("This object is not serializable!" );
			} 
			System.err.println( "While attempting to serialize, this exception occurred!" );
			System.err.println(e.getMessage()); 
		} 

		if (fDeep && !(sessClass.isInterface() || sessClass.isPrimitive() || sessClass.isArray() ) ) { 
			Field[] fields = sessClass.getDeclaredFields();
			if (fields.length > 0) { 
				System.out.println( "found "+ fields.length + " object fields:" ); 
				for (int ifield = 0; ifield < fields.length; ++ifield) { 
					System.out.println(fields[ifield].toString() ); 
				} 
			} 
			else {
				System.out.println("no object fields found"); 
			} 
		}   
	} 
}
